package com.gin.no_gay_alliance.activity;

import static com.gin.no_gay_alliance.enums.MessageWhat.REFRESH_SCAN_TASK;

import android.content.Context;
import android.content.Intent;
import android.os.Message;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;

import com.gin.no_gay_alliance.R;
import com.gin.no_gay_alliance.adpter.history.ScanTaskAdapter;
import com.gin.no_gay_alliance.app.MyApplication;
import com.gin.no_gay_alliance.database.dao.ScanTaskDao;
import com.gin.no_gay_alliance.database.entity.ScanTask;
import com.gin.no_gay_alliance.databinding.ActivityReplyHistoryScanBinding;
import com.gin.no_gay_alliance.utils.AlertDialogUtils;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 回复历史 - 扫描
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/9/21 10:32
 **/
public class ReplyHistoryScanActivity extends BaseBindingActivity<ActivityReplyHistoryScanBinding> {
    private ScanTaskAdapter adapter;

    /**
     * 解析意图
     *
     * @param intent 意图
     */
    @Override
    protected void handleIntent(Intent intent) {

    }

    /**
     * onCreate中的后续操作
     */
    @Override
    protected void onCreate() {
        adapter = new ScanTaskAdapter(this);
        b.recyclerView.setAdapter(adapter);

        handler.sendEmptyMessage(REFRESH_SCAN_TASK);
    }

    /**
     * 装载右上角菜单
     *
     * @param menu         菜单
     * @param menuInflater 菜单渲染器
     */
    @Override
    protected void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
        menuInflater.inflate(R.menu.info_menu, menu);
    }

    /**
     * 处理消息
     *
     * @param message 消息
     * @return 是否消耗该消息
     */
    @Override
    public boolean handleMessage(@NonNull Message message) {
        if (message.what == REFRESH_SCAN_TASK.ordinal()) {
            refresh();
            handler.sendEmptyMessageDelayed(REFRESH_SCAN_TASK.ordinal(), TimeUnit.SECONDS.toMillis(5));
            return true;
        }

        return super.handleMessage(message);
    }

    @Override
    protected Toolbar initToolbar() {
        return b.toolbar;
    }

    @Override
    protected void onResume() {
        super.onResume();

        refresh();
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == R.id.menu_item_info) {
            String sb = "1、主题内右上角菜单可添加扫描任务\n"
                    + "2、APP将在后台逐页记录该主题的指定页数范围内的回复内容\n"
                    + "3、扫描速度是 15s/页，如果APP进入后台可能会减慢或停止\n"
                    + "4、“监控”选项如果开启, 将以5分钟为最小间隔持续扫描该主题的最新页和次新页\n";
            AlertDialogUtils.showMessage(this, "回复历史扫描", sb, null);
        }

        return super.onOptionsItemSelected(item);
    }

    /**
     * 刷新数据
     */
    private void refresh() {
        // 查询数据
        ScanTaskDao dao = MyApplication.app.ngaDatabase.scanTaskDao();
        List<ScanTask> scanTasks = dao.listAll();
        adapter.updateData(scanTasks);
    }

    public static void startActivity(Context context) {
        context.startActivity(new Intent(context, ReplyHistoryScanActivity.class));
    }
}
